package pl.prv.polanie.client.widgets.schedule.custom;

import pl.prv.polanie.client.NativeJS;
import pl.prv.polanie.client.Planner;
import pl.prv.polanie.client.model.ServiceHolder;
import pl.prv.polanie.client.widgets.CustomDialogBox;
import pl.prv.polanie.client.widgets.PasswordTextBoxStyled;
import pl.prv.polanie.client.widgets.TextBoxStyled;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LoginPanel extends CustomDialogBox {

	
	
//	private static final String STYLE__CREATE_SUBJECT_EVENT_DIALOG = "createSubjectEventDialog";
	
	
	
	/***/
	private FlexTable widgetsFT = new FlexTable();
	
	private Label loginL= new Label("Login");
	private TextBox loginTB = new TextBoxStyled();
	
	private Label passwordL= new Label("Password");
	private PasswordTextBox passwordTB = new PasswordTextBoxStyled();

	//private Button loadDataB = new Button("load dataa");

//	private Button saveOrCreateAndCloseB = new Button("save and close");
	private Button loginB = new Button("login");
	private Button cancelB = new Button("cancel");
	
	
	
	public LoginPanel() {
		
		layoutWidgets();
		setupWidgets();

		setWidget(widgetsFT);
		setText("Login");
		
		
		
	}



	private void setupWidgets() {
		
		loginTB.setText("admin");
		passwordTB.setText("admin");
		
		
		loginB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				Planner.log("LoginPanel.onClick: " + "loguje: login=" + loginTB.getText() + ", pwd=" + passwordTB.getText());
				String login = loginTB.getText();
				String password = NativeJS.hexSha1(passwordTB.getText());
				signUp(login, password);
			}
		});
		
		cancelB.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				hideWidget();
			}
		});

		
		
//		loadDataB.addClickListener(new ClickListener() {
//			public void onClick(Widget sender) {
//				loadDataToWidgets();
//			}
//		});
	}



	private void layoutWidgets() {
		widgetsFT.setWidget(0, 0, loginL);
		widgetsFT.setWidget(0, 1, loginTB);

		widgetsFT.setWidget(1, 0, passwordL);
		widgetsFT.setWidget(1, 1, passwordTB);

		widgetsFT.getFlexCellFormatter().setColSpan(2, 0, 2);
		
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(5);
		hp.add(loginB);
		hp.add(cancelB);
		
		
		widgetsFT.setWidget(2, 0, hp);
//		widgetsFT.setWidget(2, 1, loginB);
		
		widgetsFT.getCellFormatter().setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_CENTER);
	}
	
	private void signUp(final String login, String password) {
		if (validateEnteredData()) {
			
			AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
				public void onFailure(Throwable caught) {
					Planner.log("LoginPanel.signUp() FAILURE");
				}
				public void onSuccess(Boolean res) {
					Planner.log("LoginPanel.signUp() SUCCESS -> czy zalogowano: " + res);
					hideWidget();
					
					if (res.booleanValue()) {
						Planner.get().runOnSignInAdminUser(login);
					}
				}
			};
			
			ServiceHolder.get().signInAdministrator(login, password, callback);
		}
	}

	

	private boolean validateEnteredData() {
		return true;
	}
	
	
	@Override
	public void center() {
		super.center();
		loginTB.setFocus(true);
	}
	
}